<?php


namespace app\api\controller;
use think\Controller;
use util\Excel;

class Fexcel extends Controller
{
    public function index(){
        $info = \app\functionality\model\Fexcel::get(1);
        $path = $_SERVER['DOCUMENT_ROOT'].get_file_path($info['file']);
        $datas = Excel::importexcel($path);
        $datas = array_slice($datas,0,2366);
        $data = $this->sort_arr($datas);
        $map_data = array_splice($data['data'],1);
        $map_zuobiao = array_splice($data['zuobiao'],1);
        return json(['data'=>$map_data,'zuobiao'=>$map_zuobiao,'date'=>'2020-04-10']);
    }

    public function sort_arr($arr=[]){
        $data = [];
        $zuobiao = [];
        foreach ($arr as $key=>$value){
            $data[$key]['name'] = $value[8];
            $data[$key]['value'] = $value[9];
            $zuobiao[$value[8]][0] = $value[6];
            $zuobiao[$value[8]][1] = $value[5];
        }
        return ['data'=>$data,'zuobiao'=>$zuobiao];
    }

    public function getYear(){
        $model = new \app\functionality\model\Fexcel();
        $data = $model->where('status',1)->column('year');
        $data_year = array_unique($data);
        return json(['data'=>$data_year]);
    }

    public function getMonth($year = ''){
        if (!$year){
            $year = date('Y');
        }
        $model = new \app\functionality\model\Fexcel();
        $data = $model->where('year',$year)->column('month');
        $data_month = array_unique($data);
        return json(['data'=>$data_month]);
    }

    public function getDay($year = '',$month = ''){
        if (!$year){
            $year = date('Y');
        }
        if (!$month){
            $month = date('m');
        }
        $model = new \app\functionality\model\Fexcel();
        $data_day = $model->where(['year'=>$year,'month'=>$month])->field('id,day')->select();

        //$data_day = array_unique($data);
        return json(['data'=>$data_day]);
    }

}